<template>
  <div class="hotmusic">
    <div class="top">
      <div class="hot_bg">
        <div class="hot-music"></div>
        <div class="hottime">
          <span>更新日期:{{ updateTime | formatDate }}</span>
        </div>
      </div>
    </div>
    <Loading v-if="hotMusicList.length <= 0" />
    <ul>
    <DailyHot v-for="(item,index) in hotMusicList" :item="item" :num="index" :key="item.id"></DailyHot>
    </ul>
    <div class="more-list">
      <router-link :to="`/hot/${num+1}`" tag="span">查看更多歌单</router-link>
    </div>
  </div>
</template>

<script>
import DailyHot from "../components/DailyHot";
import Loading from "../components/Loading";
export default {
  name: "HotMusic",
  components: {
    DailyHot,
    Loading,
  },
  data() {
    return {
      hotMusicList: [],
      updateTime: new Date(),
      num: 0,
    };
  },
  methods: {
    loadMusic(vm) {
      let musicids = [];
      vm.num++;
      vm.$http.get("/top/list?idx=1").then((data) => {
          vm.updateTime = data.data.playlist.updateTime;
          musicids = data.data.playlist.trackIds.slice(20*(vm.num-1),20*vm.num);
        }).then(() => {
          for (let v of musicids) {
            vm.$http.get("song/detail?ids=" + v.id).then((data) => {
              vm.hotMusicList.push({
                name: data.data.songs[0].name,
                id: data.data.songs[0].id,
                song: {
                  privilege: {
                    maxbr: data.data.privileges[0].maxbr,
                  },
                  artists: data.data.songs[0].ar,
                },
              });
            });
          }
        });
    },
  },

  beforeRouteEnter(to, from, next) {
    next(vm=>{
      vm.hotMusicList = [];
      vm.loadMusic(vm);
    });
  },
  beforeRouteUpdate(to,from,next){
    this.loadMusic(this);
    next();
  },
  filters: {
    formatDate(value) {
      let d = new Date(value);
      let m = d.getMonth() + 1;
      let day = d.getDate();
      return (
        (m < 10 ? "0" + m : m) + "月" + (day < 10 ? "0" + day : day) + "日"
      );
    },
  },
};
</script>
<style lang="less" scoped>
.hotmusic{
  margin-bottom: 50px;
  .top {
    position: relative;
    padding-top: 38.9%;
    overflow: hidden;
    background: url(../assets/hot_music_bg_2x.jpg) no-repeat;
    background-size: contain;
    .hot_bg {
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      display: flex;
      flex-direction: column;
      justify-content: center;
      padding-left: 20px;
      box-sizing: border-box;
      .hot-music {
        width: 142px;
        height: 67px;
        background: url(../assets/index_icon_2x.png) no-repeat;
        background-size: 166px 97px;
        background-position: -24px -30px;
      }
      .hottime {
        margin-top: 10px;
        color: white;
        font-size: 10px;
        text-align: left;
      }
    }
  }
  .more-list{
    span{
      color: #999999;
    }
  }
}
</style>
